package service.Imp;

import Message.DTO.CategoryDTO;
import Message.PageResult;
import Message.Prompt.MessageConstant;
import Message.Prompt.StatusConstant;
import Message.vo.CategoryPageVO;
import Message.vo.CategoryVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import mapper.CategoryMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.CategoryRelatedService;

import java.util.List;


@Slf4j
@Service
public class CategoryRelatedServiceImp implements CategoryRelatedService {


    @Autowired
    CategoryMapper categoryMapper;



    @Override
    public PageResult SelectPage(CategoryPageVO category) {
        Integer page = category.getPage();
        Integer pageSize = category.getPageSize();

        PageHelper.startPage(page, pageSize);
        //下一条sql进行分页，自动加入limit关键字分页
        List<CategoryVO> selectCategory = categoryMapper.SelectPage(category);
        
        // 使用PageInfo获取分页信息
        PageInfo<CategoryVO> pageInfo = new PageInfo<>(selectCategory);
        
        log.info("查询结果: {}", selectCategory);
        
        PageResult pageResult = new PageResult();
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setRecords(selectCategory);

        log.info("分页结果: {}", pageResult);

        return pageResult;
    }

    @Override
    public void UpdateCategory(CategoryVO category) {
        categoryMapper.UpdateCategory(category);
    }

    @Override
    public void UpdateCategoryStatus(Integer status, long id) {

        categoryMapper.UpdateCategoryStatus(status,id);

    }

    @Override
    public void DeleteCategory(Long id) {
        categoryMapper.DeleteCategory(id);
    }

    @Override
    public void AddCategory(CategoryDTO category) throws Exception {

        CategoryVO newCategory = new CategoryVO();
        BeanUtils.copyProperties(category, newCategory);

     if(newCategory.getName() == "" || newCategory.getName() == null){
         throw new Exception(MessageConstant.NAME_OR_ORDER_FAILED);
     }
        //分类状态默认为禁用状态0
        newCategory.setStatus(StatusConstant.DISABLE);
//        newCategory.setCreate_time(LocalDateTime.now());
//        newCategory.setUpdate_time(LocalDateTime.now());
//        newCategory.setCreate_user(1L);
//        newCategory.setUpdate_user(1L);

        log.info("数据",newCategory);
      categoryMapper.AddCategory(newCategory);



    }

    @Override
    public List ListCategory(Integer type) {

       List category = categoryMapper.ListCategory(type);


        return category;
    }

    @Override
    public List<CategoryVO> getCategoryList(Integer type) {

        List<CategoryVO> categoryVO =  categoryMapper.getCategoryList(type);

        return categoryVO;

    }


}
